package Reeks5.VanRansbeeck.Kevin.View;

import Reeks5.VanRansbeeck.Kevin.Model.Lang.*;
import Reeks5.VanRansbeeck.Kevin.Util.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import javax.swing.*;

public class SplashScreen extends JWindow {
    JWindow jw; // dit main window

    JButton close,start,help,settings;
    // Knoppen onder image banner
    JPanel mainFrame,paneel,flagPanel,pictFrame;
    // Verschillende panels;
    // mainFrame = wrapper
    // paneel = buttons
    // flagPanel = vlag-buttons
    // pictFrame = banner bovenaan
 
    ImageIcon nlvlag,envlag,frvlag;
    JButton nllab,enlab,frlab;
    
    String naam;
    JLabel naamlbl;
    // Welcome (taalafhankelijk) <naam>
    
    public SplashScreen() {
        this.naam = "";
        showSplashScreen(true);
        new NaamScreen(this).setVisible(true);
    }

    private void showSplashScreen(boolean show) {
        /*
         * Algemeen Window Settings 
         */
        jw = new JWindow();
        
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        jw.setBounds(100, 100, screen.width - 200, screen.height - 200);
        // JWindow is langs alle kanten van het scherm 100 pixels van schermrand

        /*
         * Eerste panel: pictFrame bevat bannerimage
         */
        pictFrame = new JPanel(new GridLayout(0,1,0,0));
        
        URL imageLoc = ClassLoader.getSystemResource("images/pokerface.jpg");
        JLabel pict = new JLabel(new ImageIcon(imageLoc), JLabel.CENTER);
        
        pictFrame.add(pict);
        
        /*
         * Tweede panel: paneel bevat alle buttons (center screen)
         */
        paneel = new JPanel(new GridLayout(0,1,0,10));
        // 1 kolom - onbekend # rijen
        // geen vertical gap, maar 10px horizontale spatie
        
        // <<-- START -->>
        start = new SplashButton(Settings.lang.getStart());
        start.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                if(ae.getSource() == start) {
                    new GameGUI(naam);
                    jw.dispose();
                }
            }
        });
        
        // addAction
        
        // <<-- HELP -->>
        help = new SplashButton(Settings.lang.getHelp());
        help.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
               if(ae.getSource() == help)
                    new HelpScreen();
            }
        });
        
        // <<-- SETTINGS -->>
        settings = new SplashButton(Settings.lang.getSettings());
        settings.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                if(ae.getSource() == settings) {
                    JFrame settingsFrame = new SettingsScreen();
                    
                    settingsFrame.setVisible(true);
                }
            }
        });
        
        // <<-- CLOSE -->>
        close = new SplashButton(Settings.lang.getClose());
        close.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                if(ae.getSource() == close)
                    System.exit(0);
            }
        });
        
        naamlbl = new JLabel(Settings.lang.getWelcome() + " " + naam, JLabel.CENTER);
        
        /*
         * Derde panel: flagPanel bevat de (3) vlaggetjes
         */
        flagPanel = new JPanel(new FlowLayout());
        
        URL urlnl = ClassLoader.getSystemResource("images/flags/nl.gif");
        URL urlen = ClassLoader.getSystemResource("images/flags/en.gif");
        URL urlfr = ClassLoader.getSystemResource("images/flags/fr.gif");
        nlvlag = new ImageIcon(urlnl); nllab = new JButton(nlvlag);
        envlag = new ImageIcon(urlen); enlab = new JButton(envlag);
        frvlag = new ImageIcon(urlfr); frlab = new JButton(frvlag);
        
        nllab.addActionListener(new ChangeLanguage());
        enlab.addActionListener(new ChangeLanguage());
        frlab.addActionListener(new ChangeLanguage());
        
        flagPanel.add(nllab);
        flagPanel.add(enlab);
        flagPanel.add(frlab);
        
        paneel.add(naamlbl);
        paneel.add(start);
        paneel.add(settings);
        paneel.add(help);
        paneel.add(close);
        
        paneel.add(flagPanel);
        
        mainFrame = new JPanel(new FlowLayout());
        
        mainFrame.add(paneel);
        
        jw.getContentPane().add(pictFrame,BorderLayout.NORTH);
        
        jw.getContentPane().add(mainFrame, BorderLayout.CENTER);
        
        jw.setVisible(true);
    }
    
    public void redraw() {
        jw.dispose();
        this.showSplashScreen(true);
    }
    
    public class ChangeLanguage extends AbstractAction {
        public void actionPerformed(ActionEvent ae) {
            if(ae.getSource() == nllab) {
                Settings.lang = new LanguageNL();
            } else if(ae.getSource() == enlab) {
                Settings.lang = new LanguageEN();
            } else if(ae.getSource() == frlab) {
                Settings.lang = new LanguageFR();
            }
            redraw();
        }
    }
    
}